package Views;

import Models.Route;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

/**
 * @author Thomas
 */
public class RouteTableModel extends DefaultTableModel
{
	private ArrayList<Route> routes;

	public RouteTableModel()
	{
		this.routes = new ArrayList<Route>();
	}

	public RouteTableModel(ArrayList<Route> routes)
	{
		this.routes = routes;
	}

	@Override
	public boolean isCellEditable(int row, int column)
	{
		return false;
	}

	public void setData(ArrayList<Route> routes)
	{
		this.routes = routes;
		fireTableDataChanged();
	}

	public ArrayList<Route> getData()
	{
		return this.routes;
	}

	@Override
	public int getColumnCount()
	{
		return 2;
	}

	@Override
	public int getRowCount()
	{
		if(this.routes == null)
		{
			return 0;
		}
		else
		{
			return this.routes.size();
		}
	}

	@Override
	public String getColumnName(int column)
	{
		String columnName = "";
		switch(column)
		{
			case 0:
				columnName = "RuteNr";
				break;
			case 1:
				columnName = "Kunder";
				break;
		}
		return columnName;
	}

	@Override
	public Object getValueAt(int row, int column)
	{
		Object value = null;
		switch(column)
		{
			case 0:
				value = this.routes.get(row).getRouteNo();
				break;
			case 1:
				String customers = "";
				int i = 1;
				while(i < 5)
				{
					customers += this.routes.get(row).getLocations().get(i).getCustomer().getName();
					if(i != 4) customers += ", ";
					i++;
				}
				value = customers;
				break;
		}
		return value;
	}
}
